/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import model.CategoriesModel;
import util.DataUtil;

/**
 *
 * @author ThanhToan
 */
public class CategoriesDAO {
    public static ArrayList<CategoriesModel> getAllCategories(){
        ArrayList<CategoriesModel> cateList = new ArrayList<CategoriesModel>();
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetAllCategories()}");
            ResultSet rs = DataUtil.executeStore(csmt);
            while(rs.next()){
                CategoriesModel cateModel = new CategoriesModel();
                cateModel.setId(rs.getString("ID"));
                cateModel.setCateName(rs.getNString("CateName"));
                cateModel.setCateDes(rs.getString("CateDes"));
                cateModel.setImage(rs.getString("Image"));
                cateList.add(cateModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cateList;
    }

    public static boolean insertCategories(CategoriesModel cateModel){
        boolean result = false;
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetInsertCategories(?,?,?)}");
            csmt.setString("cateName", cateModel.getCateName());
            csmt.setString("cateDes", cateModel.getCateDes());
            csmt.setString("Image", cateModel.getImage());
            
            result = DataUtil.executeNonStore(csmt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean updateCategories(CategoriesModel cateModel){
        boolean result = false;
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_UpdateCategories(?,?,?)}");
            csmt.setString("cateName", cateModel.getCateName());
            csmt.setString("cateDes", cateModel.getCateDes());
            csmt.setString("Image", cateModel.getImage());

            result = DataUtil.executeNonStore(csmt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
